package com.vxb.testmvp.test_zonghe.test_4;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.liaoinstan.springview.container.AliFooter;
import com.liaoinstan.springview.container.AliHeader;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;
import com.vxb.testmvp.R;
import com.vxb.testmvp.test_zonghe.entity.ZhuangEntity;
import com.vxb.testmvp.test_zonghe.network.NetworkManager;
import com.vxb.testmvp.test_zonghe.test_2.TestTowActivity;
import com.vxb.testmvp.util.Util;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

public class Test4Activity extends AppCompatActivity {
    @Bind(R.id.springview)
    SpringView springView;
    @Bind(R.id.recycler_view)
    RecyclerView recycler_view;

    private Test4Adapter adapter;
    private CompositeSubscription mCompositeSubscription;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_4);

        mCompositeSubscription = new CompositeSubscription();

        initView();

        springView.callFresh();
    }


    private void initData() {
        mCompositeSubscription.add(NetworkManager.getZhuangbiApi()
                .search("装逼")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        new Action1<List<ZhuangEntity>>() {
                            @Override
                            public void call(List<ZhuangEntity> zhuangEntities) {
                                adapter.setItems(zhuangEntities);

                            }
                        },
                        new Action1<Throwable>() {

                            @Override
                            public void call(Throwable throwable) {
                                throwable.printStackTrace();
                              //  springView.setEnable(false);
                            //    springView.setVisibility(View.GONE);
                                Util.showToast(Test4Activity.this, "setEnable false");
                            }
                        }));


    }

    private void initView() {
        ButterKnife.bind(this);

        adapter = new Test4Adapter();
        recycler_view.setLayoutManager(new GridLayoutManager(this, 2));
        recycler_view.setAdapter(adapter);
        recycler_view.setItemAnimator(new DefaultItemAnimator());


        springView = (SpringView) findViewById(R.id.springview);
       // springView.setType(SpringView.Type.OVERLAP);//必须设置这个模式 才能自动
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                        initData();
                    }
                }, 1000);
            }

            @Override
            public void onLoadmore() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        springView.onFinishFreshAndLoad();
                    }
                }, 1000);
            }
        });
        springView.setHeader(new DefaultHeader(this));
        springView.setFooter(new DefaultFooter(this));
        springView.setGive(SpringView.Give.NONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mCompositeSubscription.hasSubscriptions()) {
            mCompositeSubscription.unsubscribe();
        }
    }

}
